package com.groupfifteen.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.groupfifteen.entity.SysMultiQuestionEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SysMultiQuestionDao extends BaseMapper<SysMultiQuestionEntity> {

    /**
     * 通过试卷id 查询试卷
     *
     * @param paperId 试卷id
     * @return 问题
     */
    @Select("select * from sys_multi_question where question_id in (select question_id from sys_paper_manage where question_type" +
            " = 1 and paper_id = #{paperId})")
    List<SysMultiQuestionEntity> findByIdAndType(Integer paperId);

    /**
     * 通过id查询选择题
     *
     * @return 选择题
     */
    @Select("select * from  sys_multi_question order by question_id desc limit 1")
    SysMultiQuestionEntity findOnlyQuestionId();

    /**
     * 添加选择题
     *
     * @param multiQuestion 选择题信息
     * @return 结果
     */
    @Options(useGeneratedKeys = true, keyProperty = "question_id")
    @Insert("insert into sys_multi_question(subject,question,answer_a,answer_b,answer_c,answer_d,right_answer,analysis," +
            "section,level) " +
            "values(#{subject},#{question},#{answerA},#{answerB},#{answerC},#{answerD},#{rightAnswer},#{analysis}," +
            "#{section},#{level})")
    Integer add(SysMultiQuestionEntity multiQuestion);

    /**
     * 通过学科查询
     *
     * @param subject 学科
     * @param pageNo 分页数
     * @return 题目id
     */
    @Select("select question_id from sys_multi_question  where subject =#{subject} order by rand() desc limit #{pageNo}")
    List<Integer> findBySubject(@Param("subject") String subject, @Param("pageNo") Integer pageNo);

    /**
     * 编辑选择题
     *
     * @param multiQuestion 选择题信息
     * @return 结果
     */
    @Update("update sys_multi_question set subject = #{subject}, question = #{question}, answer_a = #{answerA}, answer_b = " +
            "#{answerB}, answer_c = #{answerC}, answer_d = #{answerD}, right_answer = #{rightAnswer}, analysis = " +
            "#{analysis}, section = #{section}, level = #{level} where question_id = #{questionId}")
    int edit(SysMultiQuestionEntity multiQuestion);

}
